#ifndef TWITCLIENT_H_
#define TWITCLIENT_H_

#include "Protocol.h"
#include "Message.h"

#include <string>
#include <string.h>
#include <pthread.h>
#include <vector>

#define SUCCESS 0
#define FAILURE -1

using namespace std;

class TwitClient{

private:

	//DATA MEMBERS

	//Consts for user defined commands
	static const string USER_COMMAND_EXIT;
	static const string USER_COMMAND_FOLLOW;
	static const string USER_COMMAND_UNFOLLOW;
	static const string USER_COMMAND_DM;
	static const string USER_COMMAND_PA;
	static const string USER_COMMAND_TWIT;
	static const string USER_COMMAND_WHO;

	//limit on the size of characters a user can send over the network
	static const int LEGAL_MESSAGE_SIZE;

	//signals to the program whether a user started an exit process
	bool _userExited;
	//indicator that all data supplied to class were valid
	bool _isReadyToConnect;
	//Indicates that the client was registered with the server successfully
	bool _isreadyToTalk;
	//a group of variables that hold network parameters of the client
	string _myName;
	struct sockaddr_in* _serverAddress;
	int _mySocketFd;
	fd_set _activeConnections;

	//FUNCTIONS

	//A function that handles user input from STDIN
	void processUserCommand();

	//A function that disconnects from the server which the client was connected to
	//The function return SUCCESS on success and FAILURE otherwise
	int disconnectFromServer();

	//signals to server to follow after Twits of another machine in the network
	//The function return SUCCESS on success and FAILURE otherwise
	int followUser(string clientName);

	//signals to server to unfollow another machine in the network
	//The function return SUCCESS on success and FAILURE otherwise
	int unfollowUser(string clientName);

	//A function that passes a twit from the user to the server
	//The function return SUCCESS on success and FAILURE otherwise
	int twit(string message);

	//A function that passes a public announcement from the user to the server
	//The function return SUCCESS on success and FAILURE otherwise
	int publicAnnouncement(string message);

	//A function that passes a direct message from the user to the server
	//The function return SUCCESS on success and FAILURE otherwise
	int directMessage(string machineID,string message);

	//A function that requests a list of all machine connected to the server this client is connected to
	//The function return SUCCESS on success and FAILURE otherwise
	int who();

	//A function that deals with incoming transmission from the server and parses it
	//The function return SUCCESS on success and FAILURE otherwise
	int receiveMessageFromServer();

	//A helper function to send messages over the network
	//The function return SUCCESS on success and FAILURE otherwise
	int executeSimpleTwitCommand(string command,vector<string> arguments,string expectedResponse);

	//A function to display twits,messages,announcements and errors to the user
	//The function return SUCCESS on success and FAILURE otherwise
	int displayTwitMessage(string message);

	//A helper function to truncate user messages to fit the network standard
	//The function returns a valid string, possibly truncated of the users' message
	string validateMessage(string message);

public:

	//FUNCTIONS

	//A constructor for the class that initializes local variables
	TwitClient(string myName,string serverAddress,short int serverPort);

	//A destructor for the class
	~TwitClient();

	//A function that starts the client session
	void runClient();

	//A function that creates the connection to the server who's details were passed the class upon instantiation
	int connectToServer();

	//A function that returns true if all network information passed to the class were valid.
	//It returns false otherwise
	bool isReadyToConnect();

};


#endif /* TWITCLIENT_H_ */


